package com.vancouver.knows.business;

import android.content.SharedPreferences.Editor;
import android.text.TextUtils;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.pangu.AMApplication;
import com.pangu.store.SharedPrefManager;
import com.vancouver.knows.business.bean.UserInfo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * SharedPreference管理器
 * <p/>
 * Created by cxlin
 */
public class BSharedPrefManager extends SharedPrefManager {

	public static final String AUID = "auid";
	public static final String USER_ID = "user_id";
	public static final String USER_NAME = "user_name";

	public static final String USER_EMAIL = "user_email";
	public static final String USER_AVATAR = "user_avatar";
	public static final String USER_LOGIN = "user_login";
	public static final String USER_POSTS_COUNT = "user_posts_count";
	public static final String USER_WATCHING_COUNT = "user_watching_count";
	public static final String USER_UNREAD_COMMENT = "user_unread_comment";
	public static final String USER_UNREAD_POST_COMMENT = "user_unread_post_comment";

	public static final String USER_PHOTO = "user_photo";
	public static final String USER_PHOTO_FROM = "user_photo_from";

	public static final String CLOSE_ALL_MESSAGE = "close_all_message";
	public static final String MY_ARTICLE_NEW_COMMENT = "my_article_new_comment";
	public static final String MY_FOLLOWED_BUSINESS_NEW_CONTENT = "my_followed_business_content";
	public static final String HAVE_AT_COMMENT_MESSAGE = "have_at_comment_message";
	public static final String NO_USER_READ_NUMBER = "no_user_read_number";
	public static final String FIRST_LOGIN = "first_login";

	public static final String LANGUAGE_WHICH = "language_which";

	public static final String TOTAL_POINTS = "total_points";
	public static final String POST_POINTS = "post_points";
	public static final String UPVOTE_POINTS = "upvote_points";
	public static final String COMMENT_POINTS = "comment_points";
	public static final String POST_UPVOTE_POINTS = "post_upvote_points";
	public static final String COMMENT_UPVOTE_POINTS = "comment_upvote_points";
	public static final String REDEEMED_POINTS = "redeemed_points";
	public static final String POINTS_POLICY = "points_policy";
	public static final String POINTS_STORE = "points_store";
	public static final String POINTS_IMAGE = "points_image";

	public static final String SEARCH_HISTORY = "search_history";
	public static final String USER_META = "user_meta";
	public static final String USER_REGISTER_TYPE = "user_register_type";
	public static final String USER_REGISTER_DATE = "user_register_date";
	public static final String USER_LISTINGS_PRIVACY_SETTING = "user_listings_privacy_setting";

	public static final String WALKTHOUGH_1 = "walkthrough_1";
	public static final String WALKTHOUGH_2 = "walkthrough_2";
	public static final String USER_ID_HASH = "user_id_hash";
	public static final String WALKTHOUGH_SPLASH_AD = "walkthrough_splash_ad";

	private AMApplication imContext;

	@SuppressWarnings("rawtypes")
	public BSharedPrefManager(AMApplication context) {
		super(context);
		imContext = context;
	}

	/**
	 * 保存用户ID
	 * 
	 * @param result
	 */
	public void saveUserId(String result) {
		Editor editor = mainPref.edit();
		editor.putString(USER_ID, result);
		editor.commit();
	}

	/**
	 * 获得用户id
	 * 
	 * @return
	 */
	public String getUserId() {
		return mainPref.getString(USER_ID, null);
	}

	/**
	 * 保存用户名
	 * 
	 * @param result
	 */
	public void saveUserName(String result) {
		Editor editor = mainPref.edit();
		editor.putString(USER_NAME, result);
		editor.commit();
	}

	/**
	 * 获得用户名
	 * 
	 * @return
	 */
	public String getUserName() {
		return mainPref.getString(USER_NAME, null);
	}

	public void saveUserEmail(String email) {
		Editor editor = mainPref.edit();
		editor.putString(USER_EMAIL, email);
		editor.commit();
	}

	public String getUserEmail() {
		return mainPref.getString(USER_EMAIL, null);
	}

	public void saveAvatar(String avatar) {
		Editor editor = mainPref.edit();
		editor.putString(USER_AVATAR, avatar);
		editor.commit();

	}

	public String getAvatar() {
		return mainPref.getString(USER_AVATAR, null);
	}

	public void saveLogin(String login) {
		Editor editor = mainPref.edit();
		editor.putString(USER_LOGIN, login);
		editor.commit();

	}

	public String getLogin() {
		return mainPref.getString(USER_LOGIN, null);
	}

	public void saveTotalUnReadComment(String totalUnReadComment) {
		Editor editor = mainPref.edit();
		editor.putString(USER_UNREAD_COMMENT, totalUnReadComment);
		editor.commit();

	}

	public String getTotalUnReadComment() {
		return mainPref.getString(USER_UNREAD_COMMENT, null);
	}

	public void saveTotalUnReadPostComment(String totalUnReadPostComment) {
		Editor editor = mainPref.edit();
		editor.putString(USER_UNREAD_POST_COMMENT, totalUnReadPostComment);
		editor.commit();

	}

	public String getTotalUnReadPostComment() {
		return mainPref.getString(USER_UNREAD_POST_COMMENT, null);
	}

	public void saveTotalPostsCount(String totalPostsCount) {
		Editor editor = mainPref.edit();
		editor.putString(USER_POSTS_COUNT, totalPostsCount);
		editor.commit();

	}

	public String getTotalPostsCount() {
		return mainPref.getString(USER_POSTS_COUNT, null);
	}

	public void saveTotalWatchingCount(String totalWatchingCount) {
		Editor editor = mainPref.edit();
		editor.putString(USER_WATCHING_COUNT, totalWatchingCount);
		editor.commit();

	}

	public String getTotalWatchingCount() {
		return mainPref.getString(USER_WATCHING_COUNT, null);
	}

	public void saveRegisterType(UserInfo.RegisterType registerType) {
		Editor editor = mainPref.edit();
		editor.putString(USER_REGISTER_TYPE, JSON.toJSONString(registerType));
		editor.commit();

	}

	public UserInfo.RegisterType getRegisterType() {
		String jsonString = mainPref.getString(USER_REGISTER_TYPE, null);
		if (jsonString == null) {
			return null;
		} else {
			return JSON.parseObject(jsonString, UserInfo.RegisterType.class);
		}
	}

	public void saveRegisterDate(String registerDate) {
		Editor editor = mainPref.edit();
		editor.putString(USER_REGISTER_DATE, registerDate);
		editor.commit();
	}

	public String getRegisterDate() {
		return mainPref.getString(USER_REGISTER_DATE, null);
	}

	public void saveUserMeta(UserInfo.UserMeta userMeta) {
		Editor editor = mainPref.edit();
		editor.putString(USER_META, JSON.toJSONString(userMeta));
		editor.commit();

	}

	public UserInfo.UserMeta getUserMeta() {
		String jsonString = mainPref.getString(USER_META, null);
		if (jsonString == null) {
			return null;
		} else {
			return JSON.parseObject(jsonString, UserInfo.UserMeta.class);
		}
	}

	public void saveUserPhoto(String userPhoto) {
		Editor editor = mainPref.edit();
		editor.putString(USER_PHOTO, userPhoto);
		editor.commit();

	}

	public String getUserPhoto() {
		return mainPref.getString(USER_PHOTO, null);
	}

	public void saveUserPhotoFrom(String from) {
		Editor editor = mainPref.edit();
		editor.putString(USER_PHOTO_FROM, from);
		editor.commit();

	}

	public String getUserPhotoFrom() {
		return mainPref.getString(USER_PHOTO_FROM, null);
	}

	// public void saveUserUnRead(boolean b) {
	// Editor editor = mainPref.edit();
	// editor.putBoolean(NO_USER_READ_NUMBER, b);
	// editor.commit();
	//
	// }
	//
	// public boolean getUserUnRead(){
	// return mainPref.getBoolean(NO_USER_READ_NUMBER, false);
	// }

	public void saveCloseAllMessage(boolean b) {
		Editor editor = mainPref.edit();
		editor.putBoolean(CLOSE_ALL_MESSAGE, b);
		editor.commit();

	}

	public boolean getCloseAllMessage() {
		return mainPref.getBoolean(CLOSE_ALL_MESSAGE, false);
	}

	public void saveMyArticleNewComment(boolean b) {
		Editor editor = mainPref.edit();
		editor.putBoolean(MY_ARTICLE_NEW_COMMENT, b);
		editor.commit();

	}

	public boolean getMyArticleNewComment() {
		return mainPref.getBoolean(MY_ARTICLE_NEW_COMMENT, false);
	}

	public void saveMyFollowedBusiness(boolean b) {
		Editor editor = mainPref.edit();
		editor.putBoolean(MY_FOLLOWED_BUSINESS_NEW_CONTENT, b);
		editor.commit();

	}

	public boolean getMyFollowedBusiness() {
		return mainPref.getBoolean(MY_FOLLOWED_BUSINESS_NEW_CONTENT, false);
	}

	public void saveHaveAtCommentMessage(boolean b) {
		Editor editor = mainPref.edit();
		editor.putBoolean(HAVE_AT_COMMENT_MESSAGE, b);
		editor.commit();

	}

	public boolean getHaveAtCommentMessage() {
		return mainPref.getBoolean(HAVE_AT_COMMENT_MESSAGE, false);
	}

	public void saveChooseLanguage(String from) {
		Editor editor = mainPref.edit();
		editor.putString(LANGUAGE_WHICH, from);
		editor.commit();

	}

	public String getChooseLanguage() {
		return mainPref.getString(LANGUAGE_WHICH, null);
	}

	/**
	 *
	 */
	public void saveAuid(String auid) {
		Editor editor = mainPref.edit();
		editor.putString(AUID, auid);
		editor.commit();
	}

	public String getAuid() {
		String auid = mainPref.getString(AUID, null);
		if (auid == null) {
			//Toast.makeText(imContext, "auid 为空！！", Toast.LENGTH_SHORT).show();
			return null;
		}
		return auid;
	}

	public void saveUserIdHash(String userIdHash) {
		Editor editor = mainPref.edit();
		editor.putString(USER_ID_HASH, userIdHash);
		editor.commit();
	}

	public String getUserIdHash() {
		String hashCode = mainPref.getString(USER_ID_HASH, null);
		if (hashCode == null) {
			//Toast.makeText(imContext, "二维码获取错误！！", Toast.LENGTH_SHORT).show();
			return null;
		}
		return hashCode;
	}

	public void saveIsFirstLogin(Boolean b){
		Editor editor = mainPref.edit();
		editor.putBoolean(FIRST_LOGIN, b);
		editor.commit();
	}

	public boolean getFirstLoginSign(){
		return mainPref.getBoolean(FIRST_LOGIN, false);
	}

	/**
	 * 总积分
	 * @param totalPoings
     */
	public void saveTotalPoints(String totalPoings){
		Editor editor = mainPref.edit();
		editor.putString(TOTAL_POINTS, totalPoings);
		editor.commit();
	}

	public String getTotalPoints(){
		return mainPref.getString(TOTAL_POINTS, null);
	}

	/**
	 * 兑换积分
	 * @param poings
     */
	public void saveRedeemedPoints(String poings){
		Editor editor = mainPref.edit();
		editor.putString(REDEEMED_POINTS, poings);
		editor.commit();
	}

	public String getRedeemedPoints(){
		return mainPref.getString(REDEEMED_POINTS, null);
	}

	/**
	 * 发表文章积分
	 * @param points
     */
	public void savePostPoints(String points) {
		Editor editor = mainPref.edit();
		editor.putString(POST_POINTS, points);
		editor.commit();
	}

	public String getPostPoints(){
		return mainPref.getString(POST_POINTS, null);
	}

	/**
	 * 点赞积分
	 * @param points
     */
	public void saveUpvotePoints(String points) {
		Editor editor = mainPref.edit();
		editor.putString(UPVOTE_POINTS, points);
		editor.commit();
	}

	public String getUpvotePoints(){
		return mainPref.getString(UPVOTE_POINTS, null);
	}

	/**
	 * 发评论积分
	 * @param points
     */
	public void saveCommentPoints(String points) {
		Editor editor = mainPref.edit();
		editor.putString(COMMENT_POINTS, points);
		editor.commit();
	}

	public String getCommentPoints(){
		return mainPref.getString(COMMENT_POINTS, null);
	}

	/**
	 * 您的文章被赞积分
	 * @param points
	 */
	public void savePostUpvotePoints(String points) {
		Editor editor = mainPref.edit();
		editor.putString(POST_UPVOTE_POINTS, points);
		editor.commit();
	}

	public String getPostUpvotePoints(){
		return mainPref.getString(POST_UPVOTE_POINTS, null);
	}

	/**
	 * 您的评论被赞积分
	 * @param points
	 */
	public void saveCommentUpvotePoints(String points) {
		Editor editor = mainPref.edit();
		editor.putString(COMMENT_UPVOTE_POINTS, points);
		editor.commit();
	}

	public String getCommentUpvotePoints(){
		return mainPref.getString(COMMENT_UPVOTE_POINTS, null);
	}

	/**
	 * 积分规则
	 * @param policyUrl
     */
	public void savePointsPolicy(String policyUrl) {
		Editor editor = mainPref.edit();
		editor.putString(POINTS_POLICY, policyUrl);
		editor.commit();
	}

	public String getPointsPolicy(){
		return mainPref.getString(POINTS_POLICY, null);
	}

	/**
	 * 积分商城
	 * @param storeUrl
     */
	public void savePointsStore(String storeUrl) {
		Editor editor = mainPref.edit();
		editor.putString(POINTS_STORE, storeUrl);
		editor.commit();
	}

	public String getPointsStore(){
		return mainPref.getString(POINTS_STORE, null);
	}

	public void savePointsImage(String points) {
		Editor editor = mainPref.edit();
		editor.putString(POINTS_IMAGE, points);
		editor.commit();
	}

	public String getImagePoints() {
		return mainPref.getString(POINTS_IMAGE, null);
	}

	public void saveSearchTerm(String searchTerm) {
		String serializedList = mainPref.getString(SEARCH_HISTORY, null);
		if (serializedList != null) {
			List<String> savedSearchTerms = new ArrayList<String>(Arrays.asList(TextUtils.split(serializedList, ",")));
			if (savedSearchTerms.contains(searchTerm)) {
				return;
			}

			savedSearchTerms.add(0, searchTerm);
			Editor editor = mainPref.edit();
			editor.putString(SEARCH_HISTORY, TextUtils.join(",", savedSearchTerms));
			editor.apply();
		} else {
			Editor editor = mainPref.edit();
			editor.putString(SEARCH_HISTORY, TextUtils.join(",", Arrays.asList(searchTerm)));
			editor.apply();
		}
	}

	public List<String> getSearchHistory() {
		String serializedList = mainPref.getString(SEARCH_HISTORY, null);
		if (serializedList != null) {
			return Arrays.asList(TextUtils.split(serializedList, ","));
		} else {
			return new ArrayList<>();
		}
	}

	public void clearSearchHistory() {
		Editor editor = mainPref.edit();
		editor.remove(SEARCH_HISTORY);
		editor.apply();
	}

	public void saveListingsPrivacySetting(String watchListPublic) {

		Editor editor = mainPref.edit();
		if (watchListPublic == null) {
			editor.putString(USER_LISTINGS_PRIVACY_SETTING, "N");
		} else {
			editor.putString(USER_LISTINGS_PRIVACY_SETTING, watchListPublic);
		}
		editor.commit();
	}

	public String getListingsPrivacySetting() {
		return mainPref.getString(USER_LISTINGS_PRIVACY_SETTING, "Y");
	}

	public void setHasSeenWalkThrough(String walkThrough) {
		Editor editor = mainPref.edit();
		editor.putBoolean(walkThrough, true);
		editor.apply();
	}

	public boolean hasSeenWalkThrough(String walkThrough) {
		return mainPref.getBoolean(walkThrough, false);
	}
}
